﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.Const;
using HF.DataModel.Data;
using System.Data.SqlClient;
using System.Configuration;
using HF.Framework;
namespace HF.WorkFlow.Engine
{
    /// <summary>
    /// 数据访问类WorkFlowInstance。
    /// </summary>
    public class WorkFlowInstance
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "WorkFlowInstanceId", "WorkFlowId", "WorkFlowNo", "FlowInstanceCaption", 
                                         "Description", "Priority", "Status", "StartTime", "EndTime", "SuspendTime",
                                         "SuspendStaus", "SuspendTotalSeconds", "IsSubWorkFlow", 
                                         "MainWorkFlowInstanceId", "MainWorkTaskInstanceId", "MainWorkTaskId", 
                                         "MainWorkFlowId","NowTaskId" };
        private string tableName = "HF_WorkFlowInstance";
        private string keyField = "WorkFlowInstanceId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkFlowInstanceEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@WorkFlowInstanceId", entity.WorkFlowInstanceId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowNo", entity.WorkFlowNo, typeof(string));
            sqlDataItem.AppendParameter("@FlowInstanceCaption", entity.FlowInstanceCaption, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@Priority", entity.Priority, typeof(string));
            sqlDataItem.AppendParameter("@Status", entity.Status, typeof(string));
            sqlDataItem.AppendParameter("@StartTime", entity.StartTime, typeof(DateTime));
            sqlDataItem.AppendParameter("@EndTime", entity.EndTime, typeof(DateTime));
            sqlDataItem.AppendParameter("@SuspendTime", entity.SuspendTime, typeof(DateTime));
            sqlDataItem.AppendParameter("@SuspendStaus", entity.SuspendStaus, typeof(string));
            sqlDataItem.AppendParameter("@SuspendTotalSeconds", entity.SuspendTotalSeconds, typeof(int));
            sqlDataItem.AppendParameter("@IsSubWorkFlow", entity.IsSubWorkFlow, typeof(bool));
            sqlDataItem.AppendParameter("@MainWorkFlowInstanceId", entity.MainWorkFlowInstanceId, typeof(string));
            sqlDataItem.AppendParameter("@MainWorkTaskInstanceId", entity.MainWorkTaskInstanceId, typeof(string));
            sqlDataItem.AppendParameter("@MainWorkTaskId", entity.MainWorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@MainWorkFlowId", entity.MainWorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@NowTaskId", entity.NowTaskId, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkFlowInstanceEntity GetWorkFlowInstanceEntity(DataRow dr)
        {
            WorkFlowInstanceEntity entity = new WorkFlowInstanceEntity();
            string value = "";
            entity.WorkFlowInstanceId = dr["WorkFlowInstanceId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkFlowNo = dr["WorkFlowNo"].ToString();
            entity.FlowInstanceCaption = dr["FlowInstanceCaption"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.Priority = dr["Priority"].ToString();
            entity.Status = dr["Status"].ToString();
            value = dr["StartTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.StartTime = Convert.ToDateTime(value);
            }
            value = dr["EndTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.EndTime = Convert.ToDateTime(value);
            }
            value = dr["SuspendTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.SuspendTime = Convert.ToDateTime(value);
            }
            entity.SuspendStaus = dr["SuspendStaus"].ToString();
            value = dr["SuspendTotalSeconds"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.SuspendTotalSeconds = Convert.ToInt32(value);
            }
            value = dr["IsSubWorkFlow"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.IsSubWorkFlow = true;
                }
                else
                {
                    entity.IsSubWorkFlow = false;
                }
            }
            entity.MainWorkFlowInstanceId = dr["MainWorkFlowInstanceId"].ToString();
            entity.MainWorkTaskInstanceId = dr["MainWorkTaskInstanceId"].ToString();
            entity.MainWorkTaskId = dr["MainWorkTaskId"].ToString();
            entity.MainWorkFlowId = dr["MainWorkFlowId"].ToString();
            entity.NowTaskId = dr["NowTaskId"].ToString();
            return entity;
        }
        public WorkTaskInstanceViewEntity GetWorkTaskInstanceViewEntity(DataRow dr)
        {
            WorkTaskInstanceViewEntity entity = new WorkTaskInstanceViewEntity();
            string value = "";
            entity.Priority = dr["Priority"].ToString();
            entity.WorkFlowNo = dr["WorkFlowNo"].ToString();
            value = dr["taskStartTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.taskStartTime = Convert.ToDateTime(value);
            }
            entity.TaskInstanceCaption = dr["TaskInstanceCaption"].ToString();
            entity.FlowInstanceCaption = dr["FlowInstanceCaption"].ToString();
            entity.OperContent = dr["OperContent"].ToString();
            entity.FlowCaption = dr["FlowCaption"].ToString();
            entity.TaskCaption = dr["TaskCaption"].ToString();
            entity.UserId = dr["UserId"].ToString();
            entity.OperatorInstanceId = dr["OperatorInstanceId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            entity.WorkFlowInstanceId = dr["WorkFlowInstanceId"].ToString();
            entity.WorkTaskInstanceId = dr["WorkTaskInstanceId"].ToString();
            value = dr["OperType"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.OperType = Convert.ToInt32(value);
            }
            entity.TaskTypeId = dr["TaskTypeId"].ToString();
          
            entity.OperatedDes = dr["OperatedDes"].ToString();
            value = dr["OperDateTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.OperDateTime = Convert.ToDateTime(value);
            }
            value = dr["taskEndTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.taskEndTime = Convert.ToDateTime(value);
            }
            value = dr["flowStartTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.flowStartTime = Convert.ToDateTime(value);
            }
            value = dr["flowEndTime"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.flowEndTime = Convert.ToDateTime(value);
            }
            entity.pOperatedDes = dr["pOperatedDes"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.OperStatus = dr["OperStatus"].ToString();
            entity.Status = dr["Status"].ToString();
            entity.taskInsType = dr["taskInsType"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkFlowInstance()
        { }
        public static WorkFlowInstance GetInstance()
        {
            return new WorkFlowInstance();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkFlowInstanceEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkFlowInstanceEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workFlowInstanceId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workFlowInstanceId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workFlowInstanceId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workFlowInstanceId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改标题
        /// </summary>
        /// <param name="workFlowInstanceId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void UpdateTitle(string workFlowInstanceId,string title)
        {
            try
            {
                string tmpSql = "Update " + tableName + " Set FlowInstanceCaption=" + title + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WorkFlowInstanceId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkFlowInstanceTable(string workFlowInstanceId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="workFlowInstanceId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkFlowInstanceEntity GetWorkFlowInstanceEntity(string workFlowInstanceId)
        {
            DataTable dt = GetWorkFlowInstanceTable(workFlowInstanceId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkFlowInstanceEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        

        #region 设置任务

        /// <summary>
        /// 设定流程实例正常结束
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例Id</param>
        public static void SetWorkFlowInstanceOver(string workFlowInstanceId)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_SetWorkFlowInstanceOverPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@workflowInsId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设定流程实例的当前位置
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例Id</param>
        /// <param name="nowtaskId">当前任务模板Id</param>
        public static void SetCurrTaskId(string workFlowInstanceId, string nowtaskId)
        {
            try
            {
                string sql = "update HF_WorkFlowInstance set nowtaskId=@nowtaskId where WorkFlowInstanceId=@WorkFlowInstanceId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@nowtaskId", nowtaskId);
                sqlItem.AppendParameter("@WorkFlowInstanceId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设定流程实例挂起
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例Id</param>
        /// <returns>操作返回码</returns>
        public static string SetSuspend(string workFlowInstanceId)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_SetWorkFlowInstanceSuspendPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@workflowInsId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                int i = agent.ExecuteNonQuery(sqlItem);
                if (i > 0)
                {
                    return WorkFlowConst.SuccessCode;
                }
                else
                {
                    return WorkFlowConst.WorkFlowSuspendErrorCode;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设定流程实例取消挂起
        /// </summary>
        ///  <param name="workFlowInstanceId">流程实例Id</param>
        /// <returns>操作返回码</returns>
        public static string SetResume(string workFlowInstanceId)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_SetWorkFlowInstanceResumePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@workflowInsId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                int i = agent.ExecuteNonQuery(sqlItem);
                if (i > 0)
                {
                    return WorkFlowConst.SuccessCode;
                }
                else
                {
                    return WorkFlowConst.WorkFlowResumeErrorCode;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设定流程实例异常终止
        /// </summary>
        ///  <param name="workFlowInstanceId">流程实例Id</param>
        ///  <param name="userId">用户IdId</param>
        ///  <param name="msg">异常终止原因</param>
        ///  <returns>操作返回码</returns>
        public static string SetAbnormal(string workflowInsId, string userId, string msg)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_SetWorkFlowInstanceAbnormalPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@workflowInsId", workflowInsId);
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@msg", msg);
                ClientDBAgent agent = new ClientDBAgent();
                int i = agent.ExecuteNonQuery(sqlItem);
                if (i > 0)
                {
                    return WorkFlowConst.SuccessCode;
                }
                else
                {
                    return WorkFlowConst.WorkFlowAbnormalErrorCode;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 待办任务
        /// <summary>
        /// 待办任务，包括新任务和已认领任务
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="topsize">返回的的记录条数</param>
        /// <returns>返回待办任务列表</returns>
        public  DataTable GetToDoWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetToDoWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 待办任务，包括新任务和已认领任务，支持分页
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>返回待办任务列表</returns>
        public DataTable GetToDoWorkTasks(string userId, int pageIndex,int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetToDoWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 指定流程类型的待办任务，支持分页
        /// </summary>
        /// <param name="userId">WorkFlowClassID</param>
        /// <param name="userId">UserId</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>返回待办任务列表</returns>
        public DataSet GetToDoWorkTasksByClass(string WorkFlowClassID,string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "sp_GetWaitingTaskListByClassID";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@WorkFlowClassID", WorkFlowClassID);
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteQuery(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 待办任务，包括新任务和已认领任务，支持分页
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>返回待办任务列表Json</returns>
        public string GetToDoWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetToDoWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt, "GetToDoWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 待办任务，包括新任务和已认领任务，支持分页
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>返回待办任务列表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetTodoWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetToDoWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;
 
        }


        /// <summary>
        /// 指定流程类型的待办任务，支持分页
        /// </summary>
        /// <param name="userId">WorkFlowClassID</param>
        /// <param name="userId">UserId</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>返回待办任务列表</returns>
        public DataSet GetTodoWorkTaskByClass(string WorkFlowClassID, string userId, int pageIndex, int pageSize)
        {
            return GetToDoWorkTasksByClass(WorkFlowClassID, userId, pageIndex, pageSize);
        }
        #endregion

        #region 未认领任务
        /// <summary>
        /// 未认领的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="topsize">返回的记录条数</param>
        /// <returns>未认领的任务列表</returns>
        public  DataTable GetClaimWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetClaimWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 未认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>未认领的任务列表</returns>
        public DataTable GetClaimWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetClaimWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 未认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>未认领的任务列表Json</returns>
        public string GetClaimWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetClaimWorkTasks(userId,pageIndex,pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt, "GetClaimWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}"; 
            }
            return result;
        }
        /// <summary>
        /// 未认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>未认领的任务列表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetClaimWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetClaimWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 我参与的任务
        /// <summary>
        /// 我参与的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>我参与的任务列表</returns>
        public  DataTable GetAllWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetMyALLWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我参与的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我参与的任务列表</returns>
        public DataTable GetAllWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetMyALLWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我参与的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我参与的任务列表Json</returns>
        public string GetAllWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetAllWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt, "GetAllWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 我参与的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我参与的任务列表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetAllWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetAllWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 我已认领的任务
        /// <summary>
        /// 我已认领的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>我已认领的任务列表</returns>
        public  DataTable GetClaimedWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetClaimedWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我已认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我已认领的任务列表</returns>
        public DataTable GetClaimedWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetClaimedWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我已认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我已认领的任务列表Json</returns>
        public string GetClaimedWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetClaimedWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt, "GetClaimedWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 我已认领的任务，支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>我已认领的任务列表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetClaimedWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetClaimedWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;
        }
        #endregion

        #region 正在运行的流程实例列表
        /// <summary>
        /// 正在运行的流程实例列表
        /// </summary>
        /// <param name="topsize">返回记录的条数</param>
        /// <returns>正在运行的流程实例列表</returns>
        public  DataTable GetRunningInstances(int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetRunningInstancePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
  
        /// <summary>
        /// 正在运行的流程实例列表，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>正在运行的流程实例列表</returns>
        public DataTable GetRunningInstances(int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetRunningInstancePagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
         /// <summary>
        /// 正在运行的流程实例列表，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>正在运行的流程实例列表Json</returns>
        public string GetRunningInstanceJson(int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetRunningInstances(pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetRunningInstanceJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 正在运行的流程实例列表，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>正在运行的流程实例列表Entitys</returns>
        public List<WorkFlowInstanceEntity> GetRunningInstanceEntity(int pageIndex, int pageSize)
        {
            List<WorkFlowInstanceEntity> result = new List<WorkFlowInstanceEntity>();
            DataTable dt = GetRunningInstances(pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkFlowInstanceEntity entity = GetWorkFlowInstanceEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 已完成的流程实例
        /// <summary>
        /// 已完成的流程实例
        /// </summary>
        /// <param name="topsize">返回记录的条数</param>
        /// <returns>已完成的流程实例列表</returns>
        public  DataTable GetCompletedInstances(int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetCompletedInstancePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 已完成的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>已完成的流程实例列表</returns>
        public DataTable GetCompletedInstances(int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetCompletedInstancePagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     
         /// <summary>
        /// 已完成的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>已完成的流程实例列表Json</returns>
        public string GetCompletedInstanceJson(int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetCompletedInstances( pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetCompletedInstanceJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 已完成的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>已完成的流程实例列表Entitys</returns>
        public List<WorkFlowInstanceEntity> GetCompletedInstanceEntity(int pageIndex, int pageSize)
        {
            List<WorkFlowInstanceEntity> result = new List<WorkFlowInstanceEntity>();
            DataTable dt = GetCompletedInstances(pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkFlowInstanceEntity entity = GetWorkFlowInstanceEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 我已完成的任务
        /// <summary>
        /// 我已完成的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>我已完成的任务表</returns>
        public  DataTable GetCompletedWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetCompletedWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我已完成的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param> 
        /// <returns>我已完成的任务表</returns>
        public DataTable GetCompletedWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetCompletedWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 指流程类型我已完成的任务,支持分页
        /// </summary>
        /// <param name="userId">WorkFlowClassID</param>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param> 
        /// <returns>我已完成的任务表</returns>
        public DataTable GetCompletedWorkTasksByClass(string WorkFlowClassID, string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetCompletedWorkTasksPageProByClassID";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@WorkFlowClassID", WorkFlowClassID);
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

         /// <summary>
        /// 我已完成的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param> 
        /// <returns>我已完成的任务表Json</returns>
        public string GetCompletedWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetCompletedWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetCompletedWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 我已完成的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param> 
        /// <returns>我已完成的任务表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetCompletedWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetCompletedWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;

        }

        /// <summary>
        /// 指流程类型我已完成的任务,支持分页
        /// </summary>
        /// <param name="userId">WorkFlowClassID</param>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param> 
        /// <returns>我已完成的任务表Entitys</returns>
        public DataTable GetCompletedWorkTaskByClass(string WorkFlowClassID, string userId, int pageIndex, int pageSize)
        {
            return GetCompletedWorkTasksByClass(WorkFlowClassID, userId, pageIndex, pageSize);
        }
        #endregion

        #region 异常终止的流程实例
        /// <summary>
        /// 异常终止的流程实例
        /// </summary>
        /// <param name="topsize">返回的记录条数</param>
        /// <returns>异常终止的任务表</returns>
        public  DataTable GetAbnormalInstances( int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetAbnormalInstancePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 异常终止的流程实例，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表</returns>
        public DataTable GetAbnormalInstances(int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetAbnormalInstancePagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
         /// <summary>
        /// 异常终止的流程实例，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表Json</returns>
        public string GetAbnormalInstanceJson(int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetAbnormalInstances(pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetAbnormalInstanceJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 异常终止的流程实例，支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表Entitys</returns>
        public List<WorkFlowInstanceEntity> GetAbnormalInstanceEntity(int pageIndex, int pageSize)
        {
            List<WorkFlowInstanceEntity> result = new List<WorkFlowInstanceEntity>();
            DataTable dt = GetAbnormalInstances(pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkFlowInstanceEntity entity = GetWorkFlowInstanceEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region  挂起的流程实例
        /// <summary>
        /// 挂起的流程实例
        /// </summary>
        /// <param name="topsize">返回的记录条数</param>
        /// <returns>挂起的任务表</returns>
        public  DataTable GetSuspendInstances(int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetSuspendInstancePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 挂起的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表</returns>
        public DataTable GetSuspendInstances(int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetSuspendInstancePagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 挂起的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表Json</returns>
        public string GetSuspendInstanceJson(int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetSuspendInstances(pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt, "GetSuspendInstanceJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
       
        /// <summary>
        /// 挂起的流程实例,支持分页
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表Entitys</returns>
        public List<WorkFlowInstanceEntity> GetSuspendInstanceEntity(int pageIndex, int pageSize)
        {
            List<WorkFlowInstanceEntity> result = new List<WorkFlowInstanceEntity>();
            DataTable dt = GetSuspendInstances(pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkFlowInstanceEntity entity = GetWorkFlowInstanceEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 异常终止的任务
        /// <summary>
        /// 异常终止的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="topsize">返回的记录条数</param>
        /// <returns>异常终止的任务表</returns>
        public  DataTable GetAbnormalWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetAbnormalWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 异常终止的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表</returns>
        public DataTable GetAbnormalWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetAbnormalWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
          /// <summary>
        /// 异常终止的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表Json</returns>
        public string GetAbnormalWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetAbnormalWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetAbnormalWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 异常终止的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常终止的任务表Entitys</returns>
        public List<WorkTaskInstanceViewEntity> GetAbnormalWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetAbnormalWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion

        #region 挂起的任务
        /// <summary>
        /// 挂起的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="topsize">返回的记录条数</param>
        /// <returns>挂起的任务表</returns>
        public  DataTable GetSuspendWorkTasks(string userId, int topsize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetSuspendWorkTasksPro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@topsize", topsize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 挂起的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表</returns>
        public DataTable GetSuspendWorkTasks(string userId, int pageIndex, int pageSize)
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_GetSuspendWorkTasksPagePro";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@pageIndex", pageIndex, typeof(int));
                sqlItem.AppendParameter("@pageSize", pageSize, typeof(int));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
         /// <summary>
        /// 挂起的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表Json</returns>
        public string GetSuspendWorkTaskJson(string userId, int pageIndex, int pageSize)
        {
            string result = ""; string totalCount;
            DataTable dt = GetSuspendWorkTasks(userId, pageIndex, pageSize);
            if (dt != null && dt.Rows.Count > 0)
            {
                totalCount = dt.Rows[0]["rowtotal"].ToString();
                string data = JsonHelper.GetInstance().DataTableToJson(dt,"GetSuspendWorkTaskJson");
                result = "{totalCount:" + totalCount + ",data:" + data + "}";
            }
            else
            {
                result = "{totalCount:0,data:[]}";
            }
            return result;
        }
        /// <summary>
        /// 挂起的任务,支持分页
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>挂起的任务表Json</returns>
        public List<WorkTaskInstanceViewEntity> GetSuspendWorkTaskEntity(string userId, int pageIndex, int pageSize)
        {
            List<WorkTaskInstanceViewEntity> result = new List<WorkTaskInstanceViewEntity>();
            DataTable dt = GetSuspendWorkTasks(userId, pageIndex, pageSize);
            foreach (DataRow dr in dt.Rows)
            {
                WorkTaskInstanceViewEntity entity = GetWorkTaskInstanceViewEntity(dr);
                result.Add(entity);
            }
            return result;

        }
        #endregion
               
        #region 任务统计
        /// <summary>
        /// 未认领任务的个数
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>数量</returns>
        public  int GetClaimTaskCount(string userId)
        {

            DataTable dt=  GetClaimWorkTasks(userId,WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;

        }
        /// <summary>
        /// 待办任务数量
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>数量</returns>
        public  int GetToDoTasksCount(string userId)
        {
            DataTable dt = GetToDoWorkTasks(userId, WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;

        }
        /// <summary>
        /// 已认领任务个数
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>数量</returns>
        public  int GetClaimedTaskCount(string userId)
        {
            DataTable dt = GetClaimedWorkTasks(userId, WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;

        }
        /// <summary>
        /// 我参与的任务个数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public  int GetAllTaskCount(string userId)
        {
            DataTable dt = GetAllWorkTasks(userId, WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;
        }
        /// <summary>
        /// 我完成的任务个数
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>数量</returns>
        public  int GetCompletedTaskCount(string userId)
        {
            DataTable dt = GetCompletedWorkTasks(userId, WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;

        }
        /// <summary>
        /// 我异常终止的任务
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>数量</returns>
        public  int GetAbnormalTaskCount(string userId)
        {
            DataTable dt = GetAbnormalWorkTasks(userId, WorkFlowConst.TaskMaxCount);
            if (dt != null)
            {
                return dt.Rows.Count;
            }
            else
                return 0;

        }
        #endregion

        #region 其他
        /// <summary>
        /// 获得流程启动者,每个流程实例只有一个启动者
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例Id</param>
        /// <returns>流程处理者</returns>
        public string GetWorkFlowStartUser(string workFlowInstanceId)
        {
            string sql = @"select oper.userId from HF_OperatorInstance oper 
                           left join HF_WorkTaskInstance taskIns on taskIns.workTaskInstanceId=oper.workTaskInstanceId
                           left join HF_WorkTask task on task.workTaskId=oper.workTaskId
                           where oper.WorkFlowInstanceId=@WorkFlowInstanceId and task.TaskTypeId=@TaskTypeId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = sql;
            sqlItem.AppendParameter("@WorkFlowInstanceId", workFlowInstanceId);
            sqlItem.AppendParameter("@TaskTypeId", WorkFlowConst.TaskType_Start, typeof(int));
            ClientDBAgent agent = new ClientDBAgent();
            return agent.ExecuteScalar(sqlItem);

        }


        
        /// <summary>
        /// 获得流程运行经过的任务历史记录
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例Id</param>
        /// <returns></returns>
        public DataTable GetWorkFlowHistory(string workFlowInstanceId)
        {
            try
            {
                string getSql = " select distinct WorkFlowId,workTaskId,workFlowInstanceId,workTaskInstanceId,operatorInstanceId," +
                               "flowcaption,taskcaption,operateddes,taskendtime from HF_WorkTaskInstanceView  " +
                               " where status='3' and workflowInstanceId=@workFlowInstanceId  order by taskendtime";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = getSql;
                sqlItem.AppendParameter("@workFlowInstanceId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程流水号，每获取一次自动加1。
        /// </summary>
        public static string GetWorkFlowNO()
        {
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "HF_CreatWorkFlowCodePro ";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                //sqlItem.AppendParameter("@Auto_Type", WorkFlowConst.WorkFlow_Flow);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得系统时间
        /// </summary>
        /// <returns>格式如2011-10-11 10:10:11</returns>
        public static string GetDBDateTime()
        {
            ClientDBAgent agent = new ClientDBAgent();
           return  agent.GetDBDateTime();
        }
        public DataTable GetTableEntity(string fromid)
        {
            string getSql = "select * from dbo.SysCustomDefineForm where cast(FormID as Varchar(50))=@FormId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = getSql;
            sqlItem.AppendParameter("@FormId", fromid);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.ExecuteDataTable(sqlItem);
        }
        /// <summary>
        /// 流程监控
        /// </summary>
        /// <param name="workFlowInstanceId"></param>
        /// <returns></returns>
        public DataTable GetWorkflowdetail(string workFlowInstanceId)
        {
            try
            {
                //string getSql = " select distinct WorkFlowNo,TaskInstanceCaption,FlowCaption,OperatedDes," +
                //               "OperDateTime,taskEndTime,flowEndTime,Message,SuccessMsg from HF_WorkTaskInstanceView  " +
                //               " where ((status='3' OR status='4' OR status='5' or Message<>'') or OperType='12') and workflowInstanceId=@workFlowInstanceId " +
                //               " and OperType <> '10' order by taskendtime";
                string getSql = " select distinct WorkFlowNo,TaskInstanceCaption,FlowCaption,OperatedDes," +
                               "taskStartTime,taskEndTime,flowEndTime,Message,SuccessMsg from HF_WorkTaskInstanceView  " +
                               " where ((status='3' OR status='4' OR status='5' or Message<>'') or OperType='12') and workflowInstanceId=@workFlowInstanceId " +
                               " and OperType <> '10' order by taskStartTime,taskEndTime";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = getSql;
                sqlItem.AppendParameter("@workFlowInstanceId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程名称
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns></returns>
        public string GetWorkFlowCaption(string workFlowId)
        {
            string sql = @"select FlowCaption from HF_WorkFlow where WorkFlowId=@WorkFlowId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = sql;
            sqlItem.AppendParameter("@WorkFlowId", workFlowId);

            ClientDBAgent agent = new ClientDBAgent();
            return agent.ExecuteScalar(sqlItem);

        }
        #endregion

        #region 流程分类归档
        /// <summary>
        /// 已完成的流程分类
        /// </summary>
        /// <returns>已完成的流程实例列表</returns>
        public DataTable GetWorkFlowInstanceClass(string url,string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                   sqlStr = @"SELECT * from WorkFlowInstanceClassView as W  where "+url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from WorkFlowInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from WorkFlowInstanceClassView";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from WorkFlowInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from WorkFlowInstanceClassView as W where " + url;
                }
                else 
                {
                    sqlStr = @"SELECT * from WorkFlowInstanceClassView as W where " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 已完成任务分类
        /// </summary>
        /// <returns>已完成任务实例列表</returns>
        public DataTable GetWorkTaskCompleteClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT * from WorkTaskCompleteClassView  where " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from WorkTaskCompleteClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from WorkTaskCompleteClassView";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from WorkTaskCompleteClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from WorkTaskCompleteClassView  where " + url;
                }
                else 
                {
                    sqlStr = @"SELECT * from WorkTaskCompleteClassView  where " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
              
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我参与的任务分类
        /// </summary>
        /// <returns>我参与的任务实例列表</returns>
        public DataTable GetWorkTaskInstanceClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT * from WorkTaskInstanceClassView  where " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from WorkTaskInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from WorkTaskInstanceClassView";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from WorkTaskInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from WorkTaskInstanceClassView  where " + url;
                }
                else 
                {
                    sqlStr = @"SELECT * from WorkTaskInstanceClassView  where " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我的申请
        /// </summary>
        /// <param name="url">sql条件</param>
        /// <param name="satae">权限状态</param>
        /// <returns></returns>
        public DataTable GetWorkFlowApplicationClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT * from HF_WorkFlowApplicationClassView as W  where " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkFlowApplicationClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from HF_WorkFlowApplicationClassView";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkFlowApplicationClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from HF_WorkFlowApplicationClassView as W where " + url;
                }
                else
                {
                    sqlStr = @"SELECT * from HF_WorkFlowApplicationClassView as W  where " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 我的审批
        /// </summary>
        /// <param name="url">sql条件</param>
        /// <param name="satae">权限状态</param>
        /// <returns></returns>
        public DataTable GetWorkFlowApproveClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT  distinct * from HF_WorkFlowApproveClassView as W where " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkFlowApproveClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from HF_WorkFlowApproveClassView as W where "+url;
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkFlowApproveClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT distinct * from HF_WorkFlowApproveClassView as W  where " + url;
                }
                else 
                {
                    sqlStr = @"SELECT distinct * from HF_WorkFlowApproveClassView as W  where " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
           /// <summary>
        /// 待办任务分类
        /// </summary>
        /// <param name="url">sql条件</param>
        /// <param name="satae">权限状态</param>
        /// <returns></returns>
        public DataTable GetTaskInstanceClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from V_HF_TaskInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from V_HF_TaskInstanceClassView";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from V_HF_TaskInstanceClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT distinct * from V_HF_TaskInstanceClassView  where " + url;
                }
                else 
                {
                    sqlStr = @"SELECT distinct * from V_HF_TaskInstanceClassView ";
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// 撤消状态
        /// </summary>
        /// <param name="WorkFlowInstanceId"></param>
        /// <param name="satae">撤消--6</param>
        /// <returns></returns>
        public bool UpdateWofkFlowInstance(string WorkFlowInstanceId, string satae)
        {
            try
            {
                string sql = "update HF_WorkFlowInstance set Status=@satae where WorkFlowInstanceId=@WorkFlowInstanceId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@satae", satae);
                sqlItem.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                if (agent.ExecuteNonQuery(sqlItem) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 作废分类统计
        /// </summary>
        /// <param name="url"></param>
        /// <param name="satae"></param>
        /// <returns></returns>
        public DataTable GetWofkFlowInstanceClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT * from HF_WorkTaskInstanceCloseView as W  where  Status = '4' and " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkTaskInstanceCloseView as W Left join
                               DatUser D on W.UserId = D.LoginName  where W.Status = '4' and D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from HF_WorkTaskInstanceCloseView where Status = '4' ";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from HF_WorkTaskInstanceCloseView as W Left join
                               DatUser D on W.UserId = D.LoginName  where  W.Status = '4' and D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from HF_WorkTaskInstanceCloseView as W  where  Status = '4' and " + url;
                }
                else
                {
                    sqlStr = @"SELECT * from HF_WorkTaskInstanceCloseView as W  where  Status = '4' and " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
              
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
           /// <summary>
        /// 删除作废流程
        /// </summary>
        /// <returns></returns>
        public bool DelectWofkFlowInstance(string WorkFlowInstanceId)
        {
            try
            {
                ClientDBAgent agent = new ClientDBAgent();
                SqlDataItem sqlItem0 = new SqlDataItem();
                SqlDataItem sqlItem1 = new SqlDataItem();
                SqlDataItem sqlItem2 = new SqlDataItem();
                SqlDataItem sqlItem3 = new SqlDataItem();
                SqlDataItem sqlItem4 = new SqlDataItem();

                sqlItem0.CommandText = "Delete HF_WorkTaskInstance where WorkFlowInstanceId=@WorkFlowInstanceId ";//删除任务实例	
                sqlItem0.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);

                sqlItem1.CommandText = "Delete HF_OperatorInstance  where WorkFlowInstanceId=@WorkFlowInstanceId "; //-删除处理者实例
                sqlItem1.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);

                sqlItem2.CommandText = "Delete Comm_AffixFiles where WorkflowInsId=@WorkFlowInstanceId "; //删除附件
                sqlItem2.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);
               
                sqlItem3.CommandText = "Delete Comm_AuditMessage where WorkflowInsId=@WorkFlowInstanceId ";  //删除审批意见
                sqlItem3.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);

                sqlItem4.CommandText = "Delete HF_WorkFlowInstance where WorkFlowInstanceId=@WorkFlowInstanceId"; //删除流程实例
                sqlItem4.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);

                agent.ExecuteNonQuery(sqlItem0);
                agent.ExecuteNonQuery(sqlItem1);
                agent.ExecuteNonQuery(sqlItem2);
                agent.ExecuteNonQuery(sqlItem3);	
                agent.ExecuteNonQuery(sqlItem4);	
				return true;
            }
            catch (Exception ex)
            {
                return false;
                throw;
            }
        }
        /// <summary>
        /// 异常流程分类统计
        /// </summary>
        /// <param name="url"></param>
        /// <param name="satae"></param>
        /// <returns></returns>
        public DataTable GetWorkFlowErrorLogsClass(string url, string satae)
        {
            try
            {
                string sqlStr = string.Empty;
                if (satae == "0")
                {
                    sqlStr = @"SELECT * from HF_ErrorLogsClassView  where LogSatus =1 and OperContentText IS NOT NULL and " + url;
                }
                else if (satae == "1")
                {
                    sqlStr = @"select W.*,D.UserID from HF_ErrorLogsClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where LogSatus =1 and OperContentText IS NOT NULL and D." + url;
                }
                else if (satae == "2")
                {
                    sqlStr = @"SELECT * from HF_ErrorLogsClassView where LogSatus =1 and OperContentText IS NOT NULL ";
                }
                else if (satae == "3")
                {
                    sqlStr = @"select W.*,D.UserID from HF_ErrorLogsClassView as W Left join
                               DatUser D on W.UserId = D.LoginName  where LogSatus =1 and OperContentText IS NOT NULL and D." + url;
                }
                else if (satae == "4")
                {
                    sqlStr = @"SELECT * from HF_ErrorLogsClassView  where LogSatus =1 and OperContentText IS NOT NULL and " + url;
                }
                else
                {
                    sqlStr = @"SELECT * from HF_ErrorLogsClassView  where LogSatus =1 and OperContentText IS NOT NULL and " + url;
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 统计流程是否阅读
        /// </summary>
        /// <param name="workFlowInstanceId"></param>
        /// <returns></returns>
        public int GetWorkFlowDisplay(string workTaskInstanceId,string userId)
        {
            try
            {
                string sqlStr = string.Empty;
                string sqlStrs = string.Empty;
                int sum = 0;
                string userIdes = "," + userId;
                sqlStrs = "select * from HF_WorkTaskInstance where WorkTaskInstanceId = @WorkTaskInstanceId"; 
                sqlStr = "update HF_WorkTaskInstance set IsDisplay = isnull(IsDisplay+'" + userIdes + "',@userIds) where WorkTaskInstanceId = @workTaskInstanceId";

                SqlDataItem sqlItems = new SqlDataItem();
                sqlItems.CommandText = sqlStrs;
                sqlItems.AppendParameter("@WorkTaskInstanceId", workTaskInstanceId);
                ClientDBAgent agents = new ClientDBAgent();
                DataTable ds = agents.ExecuteDataTable(sqlItems);

                if (ds.Rows.Count > 0 && ds.Rows[0]["IsDisplay"].ToString() != "")
                {
                    string[] split = ds.Rows[0]["IsDisplay"].ToString().Split(',');
                    foreach (string item in split)
                    {
                        if (item == userId)
                        {
                            sum = 1;
                        }
                    }
                }
                if (sum == 0 || ds.Rows[0]["IsDisplay"].ToString() == "")
                {
                    SqlDataItem sqlItem = new SqlDataItem();
                    sqlItem.CommandText = sqlStr;
                    sqlItem.AppendParameter("@workTaskInstanceId", workTaskInstanceId);
                    //sqlItem.AppendParameter("@userId", userIdes);
                    sqlItem.AppendParameter("@userIds", userId);
                    ClientDBAgent agent = new ClientDBAgent();
                    return agent.ExecuteNonQuery(sqlItem);
                }
                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查询异常流程
        /// </summary>
        /// <param name="url"></param>
        /// <param name="satae"></param>
        /// <returns></returns>
        public DataTable GetWorkFlowErrorLogs(string workFlowInstanceId)
        {
            try
            {
                string sqlStr = string.Empty;
                sqlStr = "SELECT * from HF_ErrorLogsView  where WorkFlowInstanceId =@workFlowInstanceId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@WorkFlowInstanceId", workFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 异常流程处理
        /// </summary>
        /// <returns></returns>
        public int GetWorkFlowErrorLogsColse(string type, string WorkTaskInstanceId, string WorkFlowInstanceId)
        {
          
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = "sp_HF_GetWorkFlowErrorLogsColse";
                sqlItem.CommandType = CommandType.StoredProcedure.ToString();
                sqlItem.AppendParameter("@type", type);
                sqlItem.AppendParameter("@WorkTaskInstanceId", WorkTaskInstanceId);
                sqlItem.AppendParameter("@WorkFlowInstanceId", WorkFlowInstanceId);
                ClientDBAgent agent = new ClientDBAgent();
                return  agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion


        #endregion
    }
}

